/**
 * Created by yfyuan on 2016/8/8.
 */

cBoard.directive('dashboardWidget', function ($compile, $templateCache, dataService, chartService) {

    var renderEchart = function (scope, element, attrs) {
        var template = $templateCache.get("echartContent");
        scope.myheight = scope.row.height ? (scope.row.height - 44) : 300;
        var link = $compile(template);
        element.append(link(scope));
        var ndWrapper = $(element).find('.box-body');
        scope.widget.render(ndWrapper, null, scope);
    };

    var renderMap = function (scope, element, attrs) {
        var template = $templateCache.get("chartContent");
        scope.myheight = scope.row.height ? (scope.row.height - 44) : 300;
        var link = $compile(template);
        element.append(link(scope));
        var ndWrapper = $(element).find('.box-body');
        scope.widget.render(ndWrapper, null, scope);
    };

    var renderKpi = function (scope, element, attrs) {
        var template = $templateCache.get("kpiContent");
        var aa = $compile(template)(scope);
        element.append(aa);
        var ndWrapper = $(element).find('.kpi-body');
        scope.widget.render(ndWrapper, null, scope);
    };

    var renderTable = function (scope, element, attrs) {
        var template = $templateCache.get("chartContent");
        scope.myheight = scope.row.height ? (scope.row.height - 44) : 500;
        var aa = $compile(template)(scope);
        element.append(aa);
        var ndWrapper = $(element).find('.box-body');
        scope.widget.render(ndWrapper, null, scope);
    };

    return {
        restrict: 'E',
        scope: true,
        compile: function (element, attrs) {
            return {
                pre: function (scope, element, attrs) {
                },
                post: function (scope, element, attrs) {
                    switch (scope.widget.widget.data.config.chart_type) {
                        case 'line':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'pie':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'kpi':
                            renderKpi(scope, element, attrs);
                            break;
                        case 'table':
                            renderTable(scope, element, attrs);
                            break;
                        case 'funnel':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'sankey':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'radar':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'map':
                            renderMap(scope, element, attrs);
                            break;
                        case 'scatter':
                            renderEchart(scope, element, attrs);
                            break;
                        case 'gauge':
                            renderEchart(scope, element, attrs);
                            break;
                    }
                }
            }
        }
    };
});

cBoard.directive('dynamicParameter',['$compile','$templateCache','dataService','chartService','$uibModal', function ($compile, $templateCache, dataService, chartService,$uibModal) {
    return {
        restrict: 'E',
        scope: true,
        //template:"<div  class='dy-param-box' ng-bind-html='html | to_trusted'></div><div ng-show='flagParam' style='display: inline-block;'><button type='button' ng-click='queryData(widget)'  class='btn btn-success' style='margin-right: 5px'>查询</button></div>",

        compile: function (element, attrs) {
            return {
                pre: function (scope, element, attrs) {
                },
                post: function (scope, element, attrs) {
                    scope.params = scope.widget.widget.dyParam;
                    scope.widget = scope.widget;
                    var widgetId = scope.widget.widgetId;
                    var arr = [];
                    if(!scope.params){
                        scope.flagParam=false;
                    }else{
                        scope.flagParam=true;
                    }
                    _.each(scope.params,function(item){
                        var str = "";
                        if(item.formElement=="date"){
                            str = "<li><label>"+item.text+"</label><input onClick='WdatePicker()' class='Wdate form-query' readonly  id='"+item.name+widgetId+"' type='text' name='' value='"+item.defaultValue+"' /></li>";

                        }else if(item.formElement=="text"){
                            str = "<li><label>"+item.text+"</label><input class='form-query'  id='"+item.name+widgetId+"' type='text' name='' value='"+item.defaultValue+"' /></li>";

                        }else if(item.formElement=="select"){
                            try{
                                var optionJson ={};
                                if(item.dataSource=="text"){
                                    optionJson =angular.fromJson(item.content);
                                }else{
                                    optionJson =item.data;
                                }
                                var optionArr = [];
                                optionArr.push('<option value="None">全部</option>');
                                var defaultSelect =angular.fromJson(item.defaultValue);
                                for(var o in optionJson ){
                                    if(optionJson[o].key == defaultSelect.key){
                                        optionArr.push('<option selected value="'+optionJson[o].key+'">'+optionJson[o].value+'</option>');
                                    }else{
                                        optionArr.push('<option value="'+optionJson[o].key+'">'+optionJson[o].value+'</option>');
                                    }

                                }
                                str ='<li><label>'+item.text+'</label><input type="hidden" id="'+item.name+widgetId+'_text"  value="'+defaultSelect.value+'" /><select     id="'+item.name+widgetId+'" class="form-query">'+optionArr.join("")+'</select></li>';

                            }catch(e){
                                console.error(e);
                            }
                        }else if(item.formElement=="tree"){
                            var defaultV = angular.fromJson(item.defaultValue);
                            str = '<li><label>'+item.text+'</label><input type="hidden" id="'+item.name+widgetId+'"  value="'+defaultV.key+'" /><input type="text" class="form-query"  id="'+item.name+widgetId+'_text" value="'+defaultV.value+'" readonly ng-click="open()"/></li>';
                        }
                        arr.push(str);
                        if (arr.length > 0) {
                            scope.html = arr.join("");
                            var html = "<div  class='dy-param-box'>" + scope.html + "</div><div ng-show='flagParam' style='display: inline-block;'><button type='button' ng-click='queryData(widget)'  class='btn btn-success' style='margin-right: 5px'>查询</button></div>";
                            var ele = $compile(html)(scope);
                            element.html(ele);
                        }
                        if(item.formElement=="tree") {
                            var treeData = item.data;
                                scope.open = function () {

                                    $uibModal.open({
                                        templateUrl: 'org/cboard/view/config/modal/tree.html',
                                        windowTemplateUrl: 'org/cboard/view/util/modal/window.html',
                                        backdrop: false,
                                        scope: scope,
                                        controller: function ($scope, $uibModalInstance) {
                                            $scope.originalData = treeData;
                                            $scope.treeData = [];
                                            angular.copy($scope.originalData, $scope.treeData);
                                            $scope.treeConfig = {
                                                core: {
                                                    multiple: true,
                                                    animation: true,
                                                    error: function (error) {
                                                        console.error('treeCtrl: error from js tree - ' + angular.toJson(error));
                                                    },
                                                    check_callback: true,
                                                    worker: true
                                                },
                                                types: {
                                                    default: {
                                                        icon: false
                                                    }
                                                },
                                                version: 1,
                                                plugins: ['types', 'radio', 'search']
                                            };


                                            $scope.close = function () {
                                                $uibModalInstance.close();
                                            };
                                            $scope.ok = function () {
                                                $scope.checkArray = $scope.treeInstance.jstree(true).get_selected();

                                                var obj = _.filter($scope.originalData, function (data) {
                                                    return data.id == $scope.checkArray[0];
                                                });
                                                if (obj) {
                                                    //scope.treeValue = obj[0].text;
                                                    $("#" +item.name + widgetId + "_text").val(obj[0].text);
                                                    $("#" + item.name + widgetId).val(obj[0].id);
                                                    // scope.treeId = obj[0].id;
                                                }

                                                $uibModalInstance.close();
                                            };
                                            $scope.searchNode = function () {
                                                $scope.treeInstance.jstree(true).search($scope.keywords);
                                            };
                                            $scope.myKeyup = function (e) {
                                                var keycode = window.event ? e.keyCode : e.which;
                                                if (keycode == 13) {
                                                    $scope.searchNode();
                                                }
                                            };

                                        }
                                    });
                                }
                           // }
                        }
                    });







}


            }
        }
    };
}]);
cBoard.filter('to_trusted', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
    };
}]);